package com.tbs.parkingsaleapp.UI.Listenners;

import android.content.Context;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.Toast;
import com.tbs.parkingsaleapp.Http.AsycHttp;
import com.tbs.parkingsaleapp.Http.JsonData.OrderInfo;
import com.tbs.parkingsaleapp.Http.Listenner.CancelOrderHttper;
import com.tbs.parkingsaleapp.Http.Listenner.PayHttper;
import com.tbs.parkingsaleapp.UI.DiscountApplyActivity;

public class OrderOpListenner implements View.OnLongClickListener,MenuItem.OnMenuItemClickListener {
    String[] titles={"支付订单","使用优惠券","取消订单"};
    Context context;
    OrderInfo info;
    public OrderOpListenner(Context context,OrderInfo info)
    {
        this.info=info;
        this.context=context;
    }
    @Override
    public boolean onLongClick(View v) {
        PopupMenu popupMenu=new PopupMenu(v.getContext(),v);
        Menu menu= popupMenu.getMenu();
        if(info.getBasic().getStatus()==2)
        {
            menu.add(titles[0]).setOnMenuItemClickListener(this);
            menu.add(titles[1]).setOnMenuItemClickListener(this);
        }
        if(info.getBasic().getStatus()!=1&&info.getBasic().getStatus()!=-1)
        {
            menu.add(titles[2]).setOnMenuItemClickListener(this);
        }
        popupMenu.show();
        return false;
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {

        AsycHttp<Integer,Integer> as=new AsycHttp<>();
        switch (item.getTitle().toString())
        {
            case "支付订单":
                PayHttper httper=new PayHttper(context,info);
                as.setBackgroundAction(httper).setResultAction(httper).setContext(context).execute(info.getBasic().getId());
                break;
            case "使用优惠券":
                DiscountApplyActivity.ActivityStart(context,info);

                break;
            case "取消订单":
                CancelOrderHttper cancelOrderHttper=new CancelOrderHttper(context,info);
                as.setContext(context).setBackgroundAction(cancelOrderHttper).setResultAction(cancelOrderHttper).execute(info.getBasic().getId());
                break;

            default:
                Toast.makeText(context,"命令无效",Toast.LENGTH_SHORT).show();
                break;
        }

        return false;
    }
}
